package com.csw.shuanfa.SuanFa;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomDemo1 {
    private static String AUTO_BANNER_ID = "auto@";
    private static int MAX_NUM = 5;

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        list.add("banner1");
        list.add("banner2");
        list.add("auto@banner_1");
        list.add("banner3");
        list.add("banner4");
        list.add("banner5");
        list.add("banner6");

        List<String> tempBannerList = new ArrayList<>();

        for (String str : list) {
            if (str.contains(AUTO_BANNER_ID)) {
                tempBannerList.add(str);
            }
        }
        list.removeAll(tempBannerList);
        for (String str : tempBannerList) {
            int index = getRandomIndex(list);
            list.add(index, str);
        }
        //list);

    }

    private static int getRandomIndex(List<String> list) {
        int size = Math.min(list.size() - 1, MAX_NUM);
        int randomNum = new Random().nextInt(size + 1);
        return randomNum;
    }
}
